package com.voodoo.lib_crashexception;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Author: voodoo
 * CreateDate: 2021/05/16
 * Description:
 */
public class Exception2Chinese {

    static Exception2Chinese exception2Chinese;
    static JSONObject errorJsonObject;

    public static Exception2Chinese getInstance() {
        if (exception2Chinese == null) {
            synchronized (Exception2Chinese.class) {
                if (exception2Chinese == null) {
                    exception2Chinese = new Exception2Chinese();
                }
            }
        }
        return exception2Chinese;
    }

    public Exception2Chinese() {
        if (errorJsonObject == null) {
            errorJsonObject = new JSONObject();
        }
        try {
            errorJsonObject.put("NullPointerException", "空指针异常");
            errorJsonObject.put("IndexOutOfBoundsException", "索引越界异常");
            errorJsonObject.put("ArrayIndexOutOfBoundsException", "数组下标越界异常");
            errorJsonObject.put("NumberFormatException", "字符串转数字异常");
            errorJsonObject.put("ClassNotFoundException", "找不到类异常");
            errorJsonObject.put("ConcurrentModificationException", "并发修改异常");
            errorJsonObject.put("OutOfMemoryError", "内存溢出异常");
            errorJsonObject.put("IllegalArgumentException", "非法参数异常");
            errorJsonObject.put("NoSuchFieldError", "未找到字段异常");
            errorJsonObject.put("ClassCastException", "类型强转异常");
            errorJsonObject.put("IllegalStateException", "无效状态异常");
            errorJsonObject.put("SQLiteDatabaseLockedException", "SQLite数据库锁定异常");
            errorJsonObject.put("SQLiteException", "SQLite数据库异常");
            errorJsonObject.put("InternalError", "内部异常");
            errorJsonObject.put("ActivityNotFoundException", "未找到指定Activity异常");
            // 👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆 以上为常见异常 👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆👆
            errorJsonObject.put("IllegalMonitorStateException", "违法的监控状态异常");
            errorJsonObject.put("IllegalThreadStateException", "违法的线程状态异常");
            errorJsonObject.put("NoSuchMethodException", "方法未找到异常");
            errorJsonObject.put("ArithmeticExecption", "算术异常");
            errorJsonObject.put("NegativeArrayException", "数组负下标异常");
            errorJsonObject.put("SecturityException", "违背安全原则异常");
            errorJsonObject.put("EOFException", "文件已结束异常");
            errorJsonObject.put("FileNotFoundException", "文件未找到异常");
            errorJsonObject.put("SQLException", "操作数据库异常");
            errorJsonObject.put("IOException", "输入输出异常");
            errorJsonObject.put("ArithmeticException", "算术条件异常");
            errorJsonObject.put("ArrayStoreException", "数组存储异常");
            errorJsonObject.put("CloneNotSupportedException", "不支持克隆异常");
            errorJsonObject.put("EnumConstantNotPresentException", "枚举常量不存在异常");
            errorJsonObject.put("Exception", "根异常");
            errorJsonObject.put("IllegalAccessException", "违法的访问异常");
            errorJsonObject.put("InstantiationException", "实例化异常");
            errorJsonObject.put("InterruptedException", "被中止异常");
            errorJsonObject.put("NegativeArraySizeException", "数组大小为负值异常");
            errorJsonObject.put("NoSuchFieldException", "属性不存在异常");
            errorJsonObject.put("RuntimeException", "运行时异常");
            errorJsonObject.put("SecurityException", "安全异常");
            errorJsonObject.put("StringIndexOutOfBoundsException", "字符串索引越界异常");
            errorJsonObject.put("TypeNotPresentException", "类型不存在异常");
            errorJsonObject.put("UnsupportedOperationException", "不支持的方法异常");
        } catch (JSONException ex) {
        }
    }

    public String en2ch(String exceptionStr) {
        if (exceptionStr.isEmpty()) {
            return "";
        }
        return errorJsonObject.optString(exceptionStr, String.format("未收录异常：%s", exceptionStr));
    }

}
